'use strict';
/** 直接代理保存网站 */
const https = require("https"), fs = require("fs"), zlib = require("zlib"), querystring = require("querystring");

module.exports = (request, response, rfun)=>{
    let op = {"host": "", "port": 443, "path": request.url };
    op.host = request.headers.host
    op.method = request.method;
    op.headers = request.headers;

    let req = https.request(op, (res)=>{
        if(res.headers['set-cookie']) response.setHeader("Set-Cookie", res.headers['set-cookie']);
        if(res.headers['last-modified']) response.setHeader("last-modified", res.headers['last-modified']);
        if(res.headers["content-encoding"]) response.setHeader("content-encoding", res.headers['content-encoding']);
        res.pipe(response); //console.log(res.headers);
        if(res.statusCode == 302){ //跳转
            response.statusCode = res.statusCode;
            response.setHeader("location", res.headers["location"]);
            return;
        }
        if(res.statusCode == 304){ //未修改
            response.statusCode = res.statusCode;
            return;
        }
        if(res.statusCode == 204){ //内容空
            response.statusCode = res.statusCode;
            return;
        }
        //保存网站内容
        let cd = Buffer.allocUnsafe(0)
        res.on("data", (chunk)=>{
            cd = Buffer.concat([cd, chunk], cd.length + chunk.length);
        });
        res.on("end", ()=>{
            if(res.statusCode == 200){
                const cdir = require("./cdir");
                cdir(request.SITE["root"], request.GET.pathname, ()=>{
                    if(request.GET.pathname.substr(-1, 1) == '/') request.GET.pathname += "index.html"; 
                    if(res.headers["content-encoding"] == "gzip"){
                        zlib.gunzip(cd, (err, ret)=>{
                            if(err){
                                request.LOG.error("gunzip", request.url, err);
                                return;
                            }
                            fs.writeFile(request.SITE["root"] + request.GET.pathname, ret, function(err){
                                if(err){
                                    request.LOG.error("write file error", request.url, err);
                                    return;
                                }
                                request.LOG.error("write end", request.url);
                            });
                        });
                    }else{
                        fs.writeFile(request.SITE["root"] + request.GET.pathname, cd, function(err){
                            if(err){
                                request.LOG.error("write file error", request.url, err);
                                return;
                            }
                            request.LOG.error("write end", request.url);
                        });
                    }
                });
            }else{
                request.LOG.error("request", request.url, res.statusCode, cd.length);
            }
        });
    });
    req.on("error",(err)=>{
        request.LOG.error(request.url, err);
    });
    if(request.POSTBUF) req.write(request.POSTBUF);
    req.end();
}